<template>
  <div class="mod-config">
    <el-form inline :model="dataForm">
      <el-row>
        <el-form-item>
          <el-link :href="printUrl" target="_blank" type="primary" :disabled="this.flag">
            <el-button type="primary" icon="el-icon-search" @click="searchHandle">查 询</el-button>
          </el-link>
          <el-button type="info" icon="el-icon-delete" style="margin-left: 15px;" @click="searchReset">清空</el-button>
        </el-form-item>
      </el-row>
      <el-form-item label="商品类别">
        <el-select v-model="dataForm.ctgId" @change="ctgIdChange">
          <el-option v-for="item in ctgOptions" :value="item.value" :label="item.label" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="制造商">
        <el-input v-model="dataForm.supplyName" placeholder="请选择">
          <el-button slot="append" @click="chooseSupply">选择</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="商品品种" prop="brandName">
        <el-input v-model="dataForm.brandName" readonly="readonly">
          <el-button slot="append" @click="getBrandList">选择</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="仓位">
        <el-select v-model="dataForm.stockId" v-if="this.radio != '2'">
          <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
        <el-radio-group v-model="radio" style="margin-left: 15px;" @change="changeStock">
          <el-radio label="2">不分仓</el-radio>
          <el-radio label="1">启用仓</el-radio>
          <el-radio label="0">停用仓</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <supply-search v-if="supplyVisible" ref="supplySearch" @refreshData="updateSearchForm"></supply-search>
    <select-brand v-if="selectBrandVisible" ref="selectBrand" @refreshData="updateFormBrand"></select-brand>
  </div>
</template>

<script>
  import { filterParams } from '@/util/tools'
  import { inventoryDetailFetchList } from '@/api/eims/storage'
  import { getCtgList } from '@/api/eims/ctg'
  import { getStockList } from '@/api/eims/stock'
  import SelectBrand from './SearchBrand'
  import SupplySearch from './supply-search'

  import { mapGetters } from 'vuex'
  export default {
    data() {
      return {
        printUrl: '',
        selectBrandVisible: false,
        supplyVisible: false,
        dataForm: {
          quantityCheck: 1,
          ctgId: 3,
          brandName: '',
          supplyName: ''
        },
        radio: '1',
        dataList: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        ctgOptions: [{
            label: '框镜成品镜片',
            value: 3
          },
          {
            label: '隐形成品镜片',
            value: 4
          }
        ],
        stockOptions: [],
        dataListLoading: false,
        addOrUpdateVisible: false,
        flag: true
      }
    },
    components: {
      SelectBrand,
      SupplySearch
    },
    created() {},
    computed: {
      ...mapGetters(['permissions', 'bizData'])
    },
    methods: {
      //	查询数据
      searchHandle() {
        if (!this.dataForm.brandName) {
          this.$message.warning('请选择商品品种')
          return
        }
        if (this.radio != '2') {
          if (!this.dataForm.stockId) {
            this.$message.warning('请选择仓位')
            return
          }
        }
        this.flag = false
        let params = 'stroageCheck(2d).cpt'
        let data = filterParams(this.dataForm)
        let parameter = ''
        for (let key in data) {
          parameter += '&' + key + '=' + this.dataForm[key]
        }
        this.printUrl = this.bizData.printUrl + params + parameter
      },
      //  清空查询查询表单
      searchReset() {
        for (let key in this.dataForm) {
          if (key == 'ctgId') {
            this.dataForm[key] = 3
          } else {
            this.dataForm[key] = ''
          }
        }
      },
      // 获取数据列表
      getDataList() {},
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      //商品类别改变事件
      ctgIdChange() {
        this.supplyId = ''
        this.dataForm.supplyId = ''
        this.dataForm.supplyName = ''
        this.dataForm.brandId = ''
        this.dataForm.brandName = ''
      },
      //获取制造商列表
      chooseSupply() {
        this.supplyVisible = true
        this.$nextTick(() => {
          if (this.dataForm.ctgId) {
            this.$refs.supplySearch.init(this.dataForm.ctgId)
          } else {
            this.$refs.supplySearch.init()
          }
        })
      },
      updateSearchForm(data) {
        this.dataForm.supplyId = data.id
        this.dataForm.supplyName = data.name
        this.dataForm.brandId = ''
        this.dataForm.brandName = ''
        this.dataForm.brandCode = ''
      },
      //  获取仓位列表
      getStockOptions() {
        let data = {
          status: this.radio
        }
        getStockList(data).then(response => {
          if(response.data.code == '0'){
            this.stockOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //仓位状态选择
      changeStock() {
        this.dataForm.stockId = ''
        this.getStockOptions()
      },
      // 获取商品品种信息
      getBrandList() {
        if (!this.dataForm.ctgId) {
          this.$message.warning('请选择商品类别')
          return false
        }
        if (!this.dataForm.supplyId) {
          this.$message.warning('请选择制造商')
          return false
        }
        let data = {
          ctgId: this.dataForm.ctgId,
          supplyId: this.dataForm.supplyId
        }
        this.selectBrandVisible = true
        this.$nextTick(() => {
          this.$refs.selectBrand.init(data)
        })
      },
      //  更新表单品种信息
      updateFormBrand(data) {
        this.dataForm.brandId = data.id
        this.dataForm.brandName = data.name
        this.dataForm.brandCode = data.code
        this.dataForm.name = data.name
        this.dataForm.unitType = data.unitType
      },
    }
  }
</script>
